{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3acf85e2-fa28-413b-ba92-3a6fe9194d89",
   "metadata": {},
   "source": [
    "# 第五节、数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd0a6e9d-71b1-4a04-9528-c2ad76f2160e",
   "metadata": {},
   "source": [
    "## 加减乘除幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7555c924-24ea-40d5-a314-22e1a6dc7b90",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e5cd39f0-7828-4aa3-966c-875a7ea662ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([1, 2, 3, 4])\n",
    "arr2 = np.array([2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ea049b7e-1d0a-4b17-b932-fb2046b63ebb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "546097fa-2168-4442-8753-08be22b82f1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bac26b79-b7a6-4795-ba42-e24dea8c4baa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5, 7, 9])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "af1c7a7e-a1fb-4e60-bc39-6824ebc9361b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -1, -1, -1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 - arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b4e5b5df-8b2a-4fdf-ab81-07c95cad9dfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  6, 12, 20])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * arr2  # 这不是矩阵乘法，是按位相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ab077f97-c162-4ade-aa6b-7d576e7315a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5       , 0.66666667, 0.75      , 0.8       ])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 / arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "885f91af-dbbd-4313-adb9-3b67354ba6b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 16])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1e01aeee-ab19-433e-9965-ffeeb8cc07b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1,    8,   81, 1024])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1**arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37923823-c75f-4bdc-a230-636984837837",
   "metadata": {},
   "source": [
    "## 逻辑运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bcfeb799-e8ef-49fd-90ef-2e0644a2b359",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e4912418-0fbf-45ce-a95e-45039deaa8c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False, False])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 < 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0a20e4a0-e96a-4c75-ad07-8f84515badba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False, False])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3c3d345c-826e-4ab6-afbf-27b9fd555380",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 < arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2fa2b8fe-e9ed-43d0-9955-9a73acd12f2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 != arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "589ca320-6b90-4822-883e-8d75d015232f",
   "metadata": {},
   "source": [
    "## 数组与标量计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bbb609b1-9a68-42b0-89bb-035077c36eda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e3ae3c9d-17f9-44cb-b111-ce1116f87c26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(1, 10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "886136aa-5d1c-4594-9a16-c3f834d3584a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 0.5       , 0.33333333, 0.25      , 0.2       ,\n",
       "       0.16666667, 0.14285714, 0.125     , 0.11111111])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "589295cd-a26b-42ee-8eaa-9b20e0374a2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fc229f3f-1948-420c-a79c-8059c62a0531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 10, 15, 20, 25, 30, 35, 40, 45])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr*5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbcf7bfb-4751-4f53-b61a-c71e27d73e91",
   "metadata": {},
   "source": [
    "## *=、+=、-=操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "92ca3f15-887e-4316-b1c5-99119b39dc2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr += 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "013f375c-1243-415c-ad6a-113f1ad1ce8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr  # 直接原地修改了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "14f43f67-fd9e-4da6-a687-521ca0879897",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr -= 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "11f31de7-5d7b-405c-ae3c-72b6b68dd4c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2, -1,  0,  1,  2,  3,  4,  5,  6])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "08ed3cc9-c192-4258-8275-f14cf3e2389e",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr *= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c31f120d-5c6a-4237-877f-84560c2df326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4, -2,  0,  2,  4,  6,  8, 10, 12])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f25096db-7922-4086-af73-a5cbc2d2d19c",
   "metadata": {},
   "outputs": [
    {
     "ename": "UFuncTypeError",
     "evalue": "Cannot cast ufunc 'divide' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUFuncTypeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[38], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m arr \u001b[38;5;241m/\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m   \u001b[38;5;66;03m# 没有除等\u001b[39;00m\n",
      "\u001b[1;31mUFuncTypeError\u001b[0m: Cannot cast ufunc 'divide' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'"
     ]
    }
   ],
   "source": [
    "arr /= 2   # 没有除等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8fe77a60-b866-4f77-94bd-5cf617039e0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr //= 2   # 只有除除等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2148dced-8692-412e-9aaa-40b29cf33aaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -1,  0,  0,  0,  0,  1,  1,  1])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b6130b8-ec3b-49da-af27-fbfb4ec9c0ac",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
